package com.ag.zlw.action;

import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.ag.zlw.service.LoginRecordService;
import com.ag.bean.TbLoginRecord;
import com.ag.common.PageUtil;

import excel.FormatFactory;

/**
 * 登录日志action
 * 
 * @author 钟龙伟
 * 
 */

@Scope("prototype")
@Controller("LoginRecordAction")
public class LoginRecordAction extends com.ag.common.BaseAction {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger
			.getLogger(LoginRecordAction.class);

	@Resource(name = "LoginRecordService")
	private LoginRecordService Service;
	private Map<String, String> queryOption;
	private PageUtil pageUtil;
	private List<TbLoginRecord> result;
	private String timeStart;
	private String timeEnd;
	private String message;

	public String query() {
		logger.info("LoginRecordAction");
		try {
			pageUtil = Service.query(queryOption);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "queryOK";
	}

	public String delete() {
		try {
			Service.delete(queryOption);
			this.setMessage("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			this.setMessage("删除不成功！");
		}
		return "delete";
	}

	public String output() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();

		// 从数据库中取得所要导入的数据
		System.out.println("==========timeStart=======" + timeStart);
		System.out.println("==========timeEnd=======" + timeEnd);
		result = Service.findByTime(timeStart, timeEnd);

		System.out.println("++++++++result.size()++++++++==="
				+ result.size() + "==========");
		
		// 设置资源头信息
		response.reset();
		response.setContentType("application/vnd,ms-excel");
		response.setHeader("Content-Disposition", "filename=log.xls");
		OutputStream os = response.getOutputStream();

		WritableWorkbook wb = Workbook.createWorkbook(os);
		WritableSheet sheet = wb.createSheet("sheet", 0);
		
		// 分别设置单元格的高度＆宽度
		sheet.setColumnView(0, 15);
		sheet.setColumnView(1, 15);
		sheet.setColumnView(2, 30);
		sheet.setColumnView(3, 30);
		sheet.setColumnView(4, 30);

		// 设置表中各列名称
		FormatFactory factory = new FormatFactory();
		// 在FormatFactory中调出Excle的样式
		Label label = null;
		label = new Label(0, 0, "登录用户名", factory.getFormat(1));
		sheet.addCell(label);
		label = new Label(1, 0, "用户名", factory.getFormat(1));
		sheet.addCell(label);
		label = new Label(2, 0, "登录时间", factory.getFormat(1));
		sheet.addCell(label);
		label = new Label(3, 0, "登出时间", factory.getFormat(1));
		sheet.addCell(label);
		label = new Label(4, 0, "用户角色", factory.getFormat(1));
		sheet.addCell(label);

		// 设置各列值
		if (result == null) {
			wb.write();
			wb.close();
			os.close();
		}
		if (result.size() > 0) {
			System.out.println("+++++++++++++++++==="
					+ result.iterator().next().getLrLoginTime() + "==========");
			int row = 1;
			for (row = 1; row <= result.size(); row++) {
				TbLoginRecord tbLoginRecord = new TbLoginRecord();
				tbLoginRecord = (TbLoginRecord) result.get(row - 1);

				label = new Label(0, row, tbLoginRecord.getTbUser().getUId(),
						factory.getFormat(2));
				sheet.addCell(label);
				label = new Label(1, row, tbLoginRecord.getTbUser().getUName(),
						factory.getFormat(2));
				sheet.addCell(label);

				if (tbLoginRecord.getLrLoginTime() == null) {
					label = new Label(2, row, "无", factory.getFormat(2));
					sheet.addCell(label);
				} else {
					DateFormat df = new DateFormat("yyyy-MM-dd hh:MM:ss");
					WritableCellFormat wcf = new WritableCellFormat(df);
					wcf.setAlignment(jxl.format.Alignment.CENTRE);
					DateTime labelDTF = new DateTime(2, row, tbLoginRecord
							.getLrLoginTime(), wcf);
					sheet.addCell(labelDTF);
				}
				if (tbLoginRecord.getTbLogoutTime() == null) {
					label = new Label(3, row, "无", factory.getFormat(2));
					sheet.addCell(label);
				} else {
					DateFormat df = new DateFormat("yyyy-MM-dd hh:MM:ss");
					WritableCellFormat wcf = new WritableCellFormat(df);
					wcf.setAlignment(jxl.format.Alignment.CENTRE);
					DateTime labelDTF = new DateTime(3, row, tbLoginRecord
							.getTbLogoutTime(), wcf);
					sheet.addCell(labelDTF);
				}
				label = new Label(4, row, tbLoginRecord.getTbUser()
						.getTbUserRoles().iterator().next().getTbRole()
						.getRName(), factory.getFormat(2));
				sheet.addCell(label);
			}
		}
		// 结束
		wb.write();
		wb.close();
		os.close();
		return null;
	}

	public List<TbLoginRecord> getResult() {
		return result;
	}

	public void setResult(List<TbLoginRecord> result) {
		this.result = result;
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public Map<String, String> getQueryOption() {
		return queryOption;
	}

	public void setQueryOption(Map<String, String> queryOption) {
		this.queryOption = queryOption;
	}

	public String getTimeStart() {
		return timeStart;
	}

	public void setTimeStart(String timeStart) {
		this.timeStart = timeStart;
	}

	public String getTimeEnd() {
		return timeEnd;
	}

	public void setTimeEnd(String timeEnd) {
		this.timeEnd = timeEnd;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}
